﻿using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;
using ExtendedMvc.Tests.Model;
using ExtendedMvc.Tests.Model.Entities;
using Microsoft.VisualStudio.TestTools.UnitTesting;


namespace ExtendedMvc.Tests {


	/// <summary>
	///This is a test class for PartialTest and is intended
	///to contain all PartialTest Unit Tests
	///</summary>
	[TestClass()]
	public class HtmlPartialTest {

		private const string _partialViewName = "Partial";
		private const string _partialViewContent = @"<span>Partial Content</span>";

		private static DummyDataSource database;
		private static ModelViewPage<Dummy> view;
		private static Dummy dummyModel;

		/// <summary>
		///Gets or sets the test context which provides
		///information about and functionality for the current test run.
		///</summary>
		public TestContext TestContext { get; set; }

		#region Additional test attributes
		// 
		//You can use the following additional attributes as you write your tests:
		//
		//Use ClassInitialize to run code before running the first test in the class
		[ClassInitialize()]
		public static void MyClassInitialize(TestContext testContext) {
			database = new DummyDataSource();
			dummyModel = database.Dummies.All().First();
			view = MvcMockHelpers.FakeViewPage("Index", dummyModel, "Home");
			if (!RouteTable.Routes.Any()) {
				RouteTable.Routes.MapRoute(
					"Default", // Route name
					"{controller}/{action}/{id}", // URL with parameters
					new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
				);
			}
			ViewEngines.Engines.Add(MvcMockHelpers.FakeViewEngine(_partialViewName, _partialViewContent));
		}

		//Use ClassCleanup to run code after all tests in a class have run
		//[ClassCleanup()]
		//public static void MyClassCleanup() {
		//}
		//
		//Use TestInitialize to run code before running each test
		[TestInitialize()]
		public void MyTestInitialize() {
			view.ViewContext.ViewData.ModelState.Clear();
		}

		//Use TestCleanup to run code after each test has run
		//[TestCleanup()]
		//public void MyTestCleanup() {
		//}
		//
		#endregion

		/// <summary>
		///A test for Partial Constructor
		///</summary>
		[TestMethod()]
		public void HtmlPartial_WithViewName() {
			// Arrange

			// Act
			var partial = view.Html.Partial(_partialViewName);
			var output = partial.ToString();

			// Assert
			Assert.AreEqual<string>(_partialViewContent, output);
		}

		/// <summary>
		///A test for Partial Constructor
		///</summary>
		[TestMethod()]
		public void HtmlPartial_CallRender() {
			// Arrange

			// Act
			var partial = view.Html.Partial(_partialViewName);
			var output = partial.Render();

			// Assert
			Assert.IsInstanceOfType(output, typeof(MvcHtmlString));
		}

		/// <summary>
		///A test for Partial Constructor
		///</summary>
		[TestMethod()]
		public void HtmlActionPartial_ActionName() {
			// Arrange
			var actionName = "Partial";
			MvcMockHelpers.SetViewContent(view.ViewContext.HttpContext.Server, _partialViewContent);

			// Act
			var partial = view.Html.ActionPartial(actionName);
			var output = partial.ToString();

			// Assert
			Assert.AreEqual<string>(_partialViewContent, output);
		}

		/// <summary>
		///A test for Partial Constructor
		///</summary>
		[TestMethod()]
		public void HtmlActionPartial_WithOtherActionOnly() {
			// Arrange
			var actionName = "Edit";
			MvcMockHelpers.SetViewContent(view.ViewContext.HttpContext.Server, _partialViewContent);

			// Act
			var partial = view.Html.ActionPartial(actionName);
			var output = partial.ToString();

			// Assert
			Assert.AreEqual<string>(_partialViewContent, output);
		}

		/// <summary>
		///A test for Partial Constructor
		///</summary>
		[TestMethod()]
		public void HtmlActionPartial_WithDefaultActionAndDefaultController() {
			// Arrange
			var actionName = "Index";
			var controllerName = "Home";
			MvcMockHelpers.SetViewContent(view.ViewContext.HttpContext.Server, _partialViewContent);

			// Act
			var partial = view.Html.ActionPartial(actionName, controllerName);
			var output = partial.ToString();

			// Assert
			Assert.AreEqual<string>(_partialViewContent, output);
		}

		/// <summary>
		///A test for Partial Constructor
		///</summary>
		[TestMethod()]
		public void HtmlActionPartial_WithDefaultActionAndOtherController() {
			// Arrange
			var actionName = "Edit";
			var controllerName = "Home";
			MvcMockHelpers.SetViewContent(view.ViewContext.HttpContext.Server, _partialViewContent);

			// Act
			var partial = view.Html.ActionPartial(actionName, controllerName);
			var output = partial.ToString();

			// Assert
			Assert.AreEqual<string>(_partialViewContent, output);
		}

		/// <summary>
		///A test for Partial Constructor
		///</summary>
		[TestMethod()]
		public void HtmlActionPartial_WithOtherActionAndOtherController() {
			// Arrange
			var actionName = "Edit";
			var controllerName = "Other";
			MvcMockHelpers.SetViewContent(view.ViewContext.HttpContext.Server, _partialViewContent);

			// Act
			var partial = view.Html.ActionPartial(actionName, controllerName);
			var output = partial.ToString();

			// Assert
			Assert.AreEqual<string>(_partialViewContent, output);
		}

		/// <summary>
		///A test for Partial Constructor
		///</summary>
		[TestMethod()]
		public void HtmlActionPartial_WithRouteValues() {
			// Arrange
			var actionName = "Index";
			var routeValues = new { id = 5, name = "blah" };
			MvcMockHelpers.SetViewContent(view.ViewContext.HttpContext.Server, _partialViewContent);

			// Act
			var partial = view.Html.ActionPartial(actionName, routeValues: new RouteValueDictionary(routeValues));
			var output = partial.ToString();

			// Assert
			Assert.AreEqual<string>(_partialViewContent, output);
		}

		/// <summary>
		///A test for Partial Constructor
		///</summary>
		[TestMethod()]
		public void HtmlActionPartial_WithRouteValuesAsObject() {
			// Arrange
			var actionName = "Index";
			var routeValues = new { id = 5, name = "blah" };
			MvcMockHelpers.SetViewContent(view.ViewContext.HttpContext.Server, _partialViewContent);

			// Act
			var partial = view.Html.ActionPartial(actionName, routeValues: routeValues);
			var output = partial.ToString();

			// Assert
			Assert.AreEqual<string>(_partialViewContent, output);
		}

		/// <summary>
		///A test for Partial Constructor
		///</summary>
		[TestMethod()]
		public void HtmlRoutePartial_WithRouteName() {
			// Arrange
			var routeName = "Default";
			MvcMockHelpers.SetViewContent(view.ViewContext.HttpContext.Server, _partialViewContent);

			// Act
			var partial = view.Html.RoutePartial(routeName);
			var output = partial.ToString();

			// Assert
			Assert.AreEqual<string>(_partialViewContent, output);
		}

		/// <summary>
		///A test for Partial Constructor
		///</summary>
		[TestMethod()]
		[ExpectedException(typeof(System.ArgumentException))]
		public void HtmlRoutePartial_WithInvalidRouteNameOnly() {
			// Arrange
			var routeName = "Other";

			// Act
			var partial = view.Html.RoutePartial(routeName);
			var output = partial.ToString();
		}

		/// <summary>
		///A test for Partial Constructor
		///</summary>
		[TestMethod()]
		public void HtmlRoutePartial_WithProtocol() {
			// Arrange
			var routeName = "Default";
			var protocol = "https";
			MvcMockHelpers.SetViewContent(view.ViewContext.HttpContext.Server, _partialViewContent);

			// Act
			var partial = view.Html.RoutePartial(routeName, protocol);
			var output = partial.ToString();

			// Assert
			Assert.AreEqual<string>(_partialViewContent, output);
		}

		/// <summary>
		///A test for Partial Constructor
		///</summary>
		[TestMethod()]
		public void HtmlRoutePartial_WithHostName() {
			// Arrange
			var routeName = "Default";
			var hostName = "server";
			MvcMockHelpers.SetViewContent(view.ViewContext.HttpContext.Server, _partialViewContent);

			// Act
			var partial = view.Html.RoutePartial(routeName, hostName: hostName);
			var output = partial.ToString();

			// Assert
			Assert.AreEqual<string>(_partialViewContent, output);
		}

		/// <summary>
		///A test for Partial Constructor
		///</summary>
		[TestMethod()]
		public void HtmlRoutePartial_WithRouteValues() {
			// Arrange
			var routeName = "Default";
			var routeValues = new { id = 5, name = "blah" };
			MvcMockHelpers.SetViewContent(view.ViewContext.HttpContext.Server, _partialViewContent);

			// Act
			var partial = view.Html.RoutePartial(routeName, routeValues: new RouteValueDictionary(routeValues));
			var output = partial.ToString();

			// Assert
			Assert.AreEqual<string>(_partialViewContent, output);
		}

		/// <summary>
		///A test for Partial Constructor
		///</summary>
		[TestMethod()]
		public void HtmlRoutePartial_WithRouteValuesAsObject() {
			// Arrange
			var routeName = "Default";
			var routeValues = new { id = 5, name = "blah" };
			MvcMockHelpers.SetViewContent(view.ViewContext.HttpContext.Server, _partialViewContent);

			// Act
			var partial = view.Html.RoutePartial(routeName, routeValues: routeValues);
			var output = partial.ToString();

			// Assert
			Assert.AreEqual<string>(_partialViewContent, output);
		}
	}
}
